﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    public class DirectUIControl
    {
        private DockStyle dock = DockStyle.None;
        internal Rectangle border;
        private DirectUIControl parent;
        private Size parentSize;
        internal System.Windows.Forms.Control control;


        /// <summary>
        /// 获取或设置控件左边缘与其容器的工作区左边缘之间的距离（以像素为单位）。
        /// </summary>
        /// <returns>System.Int32 表示控件左边缘与其容器的工作区左边缘之间的距离（以像素为单位）。</returns>
        public int Left { get; set; }

        /// <summary>
        /// 获取或设置控件上边缘与其容器的工作区上边缘之间的距离（以像素为单位）。
        /// </summary>
        /// <returns>System.Int32 表示控件下边缘与其容器的工作区上边缘之间的距离（以像素为单位）。</returns>
        public int Top { get; set; }

        /// <summary>
        /// 获取或设置控件的宽度。
        /// </summary>
        /// <returns>控件的宽度（以像素为单位）。</returns>
        public int Width { get; set; }

        /// <summary>
        /// 获取或设置控件的高度。
        /// </summary>
        /// <returns>控件的高度（以像素为单位）。</returns>
        public int Height { get; set; }

        /// <summary>
        /// 获取或设置控件的名称。
        /// </summary>
        /// <returns>控件的名称。 默认值为空字符串("")。</returns>
        public string Name { get; set; }

        /// <summary>
        /// 获取或设置该控件的左上角相对于其容器的左上角的坐标。
        /// </summary>
        /// <returns>System.Drawing.Point，表示控件的左上角相对于其容器的左上角。</returns>
        public Point Location
        {
            get => new Point(this.Left, this.Top);
            set
            {
                this.Left = value.X;
                this.Top = value.Y;
            }
        }

        /// <summary>
        /// 获取或设置控件的高度和宽度。
        /// </summary>
        /// <returns>System.Drawing.Size，表示控件的高度和宽度（以像素为单位）。</returns>
        public Size Size
        {
            get => new Size(this.Width, this.Height);
            set
            {
                this.Width = value.Width;
                this.Height = value.Height;
            }
        }

        /// <summary>
        /// 获取或设置控件的背景色。
        /// </summary>
        /// <returns>表示控件背景色的 System.Drawing.Color。 默认为 System.Windows.Forms.Control.DefaultBackColor 属性的值。</returns>
        public virtual Color BackColor { get; set; } = System.Windows.Forms.Control.DefaultBackColor;

        /// <summary>
        /// 获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。
        /// </summary>
        /// <returns>System.Windows.Forms.DockStyle 值之一。 默认值为 System.Windows.Forms.DockStyle.None。</returns>
        [DefaultValue(typeof(DockStyle), "None")]
        public virtual DockStyle Dock
        {
            get => dock; set
            {
                dock = value;
                if (this.Dock != DockStyle.None && this.Parent is not null)
                {
                    this.parent.SizeChanged += this.Parent_SizeChanged;
                    refreshLayout();
                }
                else if (this.Dock == DockStyle.None)
                    this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            }
        }

        /// <summary>
        /// 获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。
        /// </summary>
        /// <returns>System.Windows.Forms.AnchorStyles 值的按位组合。 默认值是 Top 和 Left。</returns>
        [DefaultValue(AnchorStyles.Top | AnchorStyles.Left)]
        public virtual AnchorStyles Anchor { get; set; } = AnchorStyles.Top | AnchorStyles.Left;

        public event EventHandler SizeChanged;


        public virtual void Bind(System.Windows.Forms.Control control)
        {
            this.control = control;
            this.Size = this.control.Size;
            this.BackColor = this.control.BackColor;
            this.control.SizeChanged += this.Control_SizeChanged;
            this.control.BackColorChanged += this.Control_BackColorChanged;
        }

        private void Control_BackColorChanged(object sender, EventArgs e)
        {
            this.BackColor = this.control.BackColor;
        }

        private void Control_SizeChanged(object sender, EventArgs e)
        {
            this.refreshLayout();
            OnSizeChanged(e);
        }

        public virtual void OnSizeChanged(EventArgs e)
        {
            this.SizeChanged?.Invoke(this, e);
        }

        /// <summary>
        /// 刷新布局
        /// </summary>
        internal virtual void refreshLayout()
        {
            Size size;
            if (this.parent is not null)
                size = this.parent.Size;
            else if (this.control is not null)
                size = this.control.Size;
            else
                return;
            switch (this.Dock)
            {
                case DockStyle.None:
                    if (parentSize == default)
                        break;
                    if (this.Anchor == AnchorStyles.None)
                    {
                        this.Left += (size.Width - parentSize.Width) / 2;
                        this.Top += (size.Height - parentSize.Height) / 2;
                        break;
                    }
                    if (this.Anchor.HasFlag(AnchorStyles.Right))
                    {
                        if (this.Anchor.HasFlag(AnchorStyles.Left))
                            this.Width += size.Width - parentSize.Width;
                        else
                            this.Left += size.Width - parentSize.Width;
                    }
                    if (this.Anchor.HasFlag(AnchorStyles.Bottom))
                    {
                        if (this.Anchor.HasFlag(AnchorStyles.Top))
                            this.Height += size.Height - parentSize.Height;
                        else
                            this.Top += size.Width - parentSize.Width;
                    }
                    break;
                case DockStyle.Top:
                    this.Left = 0;
                    this.Top = 0;
                    this.Width = size.Width;
                    break;
                case DockStyle.Bottom:
                    this.Width = size.Width;
                    this.Top = size.Height - this.Height;
                    break;
                case DockStyle.Left:
                    this.Left = 0;
                    this.Top = 0;
                    this.Height = size.Height;
                    break;
                case DockStyle.Right:
                    this.Left = size.Width - this.Width;
                    this.Height = size.Height;
                    break;
                case DockStyle.Fill:
                    this.Left = 0;
                    this.Top = 0;
                    this.Width = size.Width;
                    this.Height = size.Height;
                    break;
            }
            parentSize = size;
        }
        public void Invalidate()
        {
        }

        /// <summary>
        /// 获取或设置控件的父容器。
        /// </summary>
        /// <returns> Marmat.Forms.Skin.DirectUIControl，表示控件的父控件或容器控件。</returns>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DirectUIControl Parent
        {
            get => parent; set
            {
                parent = value;
                if (this.Dock != DockStyle.None && value is not null)
                {
                    this.parent.SizeChanged += this.Parent_SizeChanged;
                    refreshLayout();
                }
            }
        }

        private void Parent_SizeChanged(object sender, EventArgs e)
        {
            refreshLayout();
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseClick 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        public virtual void OnMouseClick(MouseEventArgs e)
        {
            MessageBox.Show($"{this.Name} - {e.Location}");
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseDoubleClick 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected virtual void OnMouseDoubleClick(MouseEventArgs e)
        {
        }

        protected virtual void OnMouseEnter(EventArgs e)
        {
        }

        protected virtual void OnMouseLeave(EventArgs e)
        {
        }

        protected virtual void OnMouseMove(MouseEventArgs e)
        {
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseDown 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        protected virtual void OnMouseDown(MouseEventArgs e)
        {
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.MouseUp 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        protected virtual void OnMouseUp(MouseEventArgs e)
        {
        }

        protected virtual void OnMouseHover(EventArgs e)
        {
        }

        protected virtual void OnMouseWheel(MouseEventArgs e)
        {
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Control.Paint 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 System.Windows.Forms.PaintEventArgs。</param>
        protected virtual void OnPaint(PaintEventArgs e)
        {
            using Brush brush = new SolidBrush(this.BackColor);
            e.Graphics.FillRectangle(brush, e.ClipRectangle);
            if (this.Name == "1")
                Console.WriteLine(e.ClipRectangle);
        }

        public void Draw(PaintEventArgs e)
        {
            this.OnPaint(e);
        }
    }
}
